package com.touzbi.hss.bo;

import javax.servlet.http.HttpServletRequest;

import com.touzbi.hss.beans.Patient;
import com.touzbi.hss.beans.Soin;
import com.touzbi.hss.dao.AbstractDAOFactory;
import com.touzbi.hss.dao.AbstractPatientDAO;
import com.touzbi.hss.dao.DAOException;
import com.touzbi.hss.parametermanager.AbstractParameterManager;
import com.touzbi.hss.parametermanager.HttpParameterManager;

public class PatientBO {

	private static final String ID_PARAM = "id";
	private AbstractPatientDAO patientDAO;
	private AbstractParameterManager paramManager;
	private String error = null;

	private HttpServletRequest req;

	public PatientBO(AbstractDAOFactory daoFactory, HttpServletRequest req) {
		this.patientDAO = daoFactory.getPatientDAO();
		this.paramManager = new HttpParameterManager(req);
		this.req = req;

	}

	public Patient getPatient(HttpServletRequest req) {
		String id = this.paramManager.getParamValue(ID_PARAM);
		Patient patient = null;
		try {
			patient = this.patientDAO.getPatientById(Long.valueOf(id));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (DAOException e) {
			this.error = e.getMessage();
		}
		return patient;

	}

	public void addSoin(HttpServletRequest req, Soin soin) {
		this.patientDAO.addSoin(soin, getPatient(req));
	}

	public void addSoin(HttpServletRequest req, Soin soin, Patient patient) {
		this.patientDAO.addSoin(soin, patient);
	}

	public String getError() {
		return error;
	}
}
